package com.sts.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.sts.bean.Base_eq;
import com.sts.bean.User;
import com.sts.bean.User1;
import com.sts.service.IUserService;

@Controller
public class UserController {
	
	
	@Resource
	private IUserService ius ;
	
	private Logger log = Logger.getLogger(this.getClass()) ;
	
	/**
	 * 查询登录信息
	 */
	@RequestMapping(value="/loginA", produces="text/html;charset=UTF-8")
	@ResponseBody
	public String loginA(String user_name,String user_password,HttpSession session){
		log.debug("loginA controller in");
		
		log.debug("user_name" + user_name + ",user_password:" + user_password);
		String str = "" ;
		User1 user = ius.isUser(user_name, user_password) ;
		log.debug("hhdsf"+user);
		if(user != null){
			session.setAttribute("user_name", user_name);
			session.setAttribute("id", user.getId());
			session.setAttribute("deg", user.getUser_power());
			session.setMaxInactiveInterval(10);		//设置session的过时时间，单位是秒
			str = "success" ;
		}else{
			str = "flase" ;
		}
		
		log.debug("loginA controller in");
		return str ;
	}
	
	/**
	 * 查询所有的人员
	 */
	@RequestMapping(value="/selectUserAll", produces="text/html;charset=UTF-8")
	@ResponseBody
	public String selectUserAll(String page ,String limit ,String id ,String user_name){
		log.debug("selectUserAll controller in");
		
		log.debug("user_name:" + user_name + ",id:" + id);
		String str = "" ;
		List<User1> list = ius.selectUserListS(id, user_name) ;
		int length = ius.selectSize(id, user_name);
		str = JSON.toJSONString(list);
		log.debug("list:" + list);
		log.debug("length:" + length);
		str= "{\"code\":0,\"msg\":\"  \",\"count\":\""+length+"\",\"data\":"+str+"}";
		log.debug("selectUserAll controller out");
		
		return str ;
	}
	/**
	 * 查询所有的人员
	 */
	@RequestMapping(value="/selectById", produces="text/html;charset=UTF-8")
	@ResponseBody
	public String selectById(String id,String user_name){
		log.debug("selectById controller in");
		
		String str = "" ;
		log.debug("id:" + id);
		List<User1> list = ius.selectUserListS(id, user_name) ;
		//int length = ius.selectSize(id, user_name);
		str = JSON.toJSONString(list);
		if(user_name != null){
			str= "{\"code\":0,\"msg\":\"  \",\"count\":\""+ 1 +"\",\"data\":"+str+"}";
		}
		log.debug("list:" + list);
		//log.debug("length:" + length);
		log.debug("selectById controller out");
		
		return str ;
	}
	/**
	 * 根据id删除人员
	 */
	@RequestMapping(value="/deleteUserById", produces="text/html;charset=UTF-8")
	@ResponseBody
	public String deleteUserById(String id ){
		log.debug("deleteUserById controller in");
		log.debug(",id:" + id);
		String str = "" ;
		
		if(ius.deleteUserS(id)){
			str = "success" ;
		}else{
			str = "false" ;
		}
		
		
		log.debug("deleteUserById controller in");
		return str ;
	}
	
	
	/**
	 * 插入用户信息
	 * @param data
	 * @return
	 */
	@RequestMapping("/add_user")
	@ResponseBody
	public String add_user(String data){
		log.debug("add_user controller in");
		String result = "" ;
		log.debug("数据" + data);
		//将JSON对象转换为对象
		User1 user = JSON.parseObject(data,User1.class) ;
		log.debug(user.toString());
		int b = ius.insertUser(user) ;
		if(b == 1){
			result = "success" ;
		}else {
			result = "false" ;
		}
		
		
		log.debug("add_user controller out");
		return result;
	}
	/**
	 * 访问userAdd页面
	 * 
	 */
	
	@RequestMapping("/userAdd")
	public String userAdd(){
		
		log.debug("userAdd  controller in");
		
		log.debug("userAdd  controller out");
		return "userAdd";
	}
	
	/**
	 * 返回修改页面
	 */
	@RequestMapping(value="/user_revise", produces="text/html;charset=UTF-8")
	public String user_revise(String id,String vs,HttpSession session){
		log.debug("user_revise controller in");
		
		log.debug("revise_id:" + id+ " ,vs:" + vs );
		String str = "" ;
		
		
		
		session.setAttribute("revise_id", id);
		session.setAttribute("vs", vs);
		str = "user_revise" ;
		
		
		log.debug("user_revise controller in");
		return str ;
	}
	/**
	 * 更新用户信息
	 * @param data
	 * @return
	 */
	@RequestMapping("/updateUser")
	@ResponseBody
	public String updateUser(String data){
		log.debug("updateUser controller in");
		String result = "" ;
		log.debug("数据" + data);
		//将JSON对象转换为对象
		User1 user = JSON.parseObject(data,User1.class) ;
		log.debug(user.toString());
		int b = ius.updatetUser(user) ;
		if(b == 1){
			result = "success" ;
		}else {
			result = "false" ;
		}
		
		
		log.debug("updateUser controller out");
		return result;
	}
	
}
